<?php

namespace Kangcg\Application\EventSource;

class Event
{
    private $comments = [];
    private $id;
    private $event;
    private $retry;
    private $data = [];

    public function addComment($comment)
    {
        $this->comments = array_merge(
            $this->comments,
            $this->extractNewlines($comment)
        );

        return $this;
    }

    public function setId(string $id)
    {
        $this->id = $id;
        return $this;
    }

    public function setEvent(string $event)
    {
        $this->event = $event;
        return $this;
    }

    public function setRetry(int $retry)
    {

        $this->retry = $retry;
        return $this;
    }

    public function setData($data)
    {
        $this->data = $this->extractNewlines($data);
        return $this;
    }

    public function appendData($data)
    {
        $this->data = array_merge(
            $this->data,
            $this->extractNewlines($data)
        );

        return $this;
    }

    public function dump()
    {
        $response = $this->getFormattedComments().
            $this->getFormattedId().
            $this->getFormattedEvent().
            $this->getFormattedRetry().
            $this->getFormattedData();

        return '' !== $response ? $response."\n" : '';
    }

    public function getFormattedComments()
    {
        return $this->formatLines('', $this->comments);
    }

    public function getFormattedId()
    {
        return $this->formatLines('id', $this->id);
    }

    public function getFormattedEvent()
    {
        return $this->formatLines('event', $this->event);
    }

    public function getFormattedRetry()
    {
        return $this->formatLines('retry', $this->retry);
    }

    public function getFormattedData()
    {
        return $this->formatLines('data', $this->data);
    }

    private function extractNewlines($input)
    {
        return explode("\n", $input);
    }

    private function formatLines($key, $lines)
    {
        $formatted = array_map(
            function ($line) use ($key) {
                return $key.': '.$line."\n";
            },

            (array) $lines
        );

        return implode('', $formatted);
    }

    static public function create(){
        return new static();
    }
}
